//编辑菜单
var MenuId ;

/*菜单相关操作 start*/
//获取菜单列表
var table = $("#menu_List").dataTable({
    //"iDisplayLength":10,
    "ajax": {
        "url":"/role/get-menu-list",
        "type":'post',
        "data": function (d) {
            //添加额外的参数传给服务器
            //return $.extend({ CurrentPage:0, PageSize: 10}, {});
        }},
    'bPaginate': true,
    "bDestory": true,
    "bRetrieve": true,
    "bFilter":false,
    "bSort": true,
    "bProcessing": true,
    "autoWidth": false,
    "searching":true,
    "aoColumns": [
        {"mDataProp": "Sort"},
        {"mDataProp": "MenuName"},
        {"mDataProp": "UrlAction"},
        {
            "mDataProp": "MenuId",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html('<button name="btnEdit" type="button" class="btn btn-xs btn-info" '+
                "onclick='_editFun(\"" + oData.MenuId + "\",\"" + oData.MenuName + "\",\"" + oData.UrlAction + "\",\"" + oData.CssClass + "\",\"" + oData.Sort + "\")'><i class='fa fa-edit'></i>编辑</button>&nbsp;&nbsp;")
                    .append("<button name='btnSetting' type='button' class='btn btn-default btn-xs'  onclick='GetViewList(" + oData.MenuId + ")'><i class='fa fa-cog'></i>设置</button>");
            }
        },
    ],
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
        //add selected class
        $(nRow).click(function () {
            if ($(this).hasClass('row_selected')) {
                $(this).removeClass('row_selected');
            } else {
                table.$('tr.row_selected').removeClass('row_selected');
                $(this).addClass('row_selected');
            }
        });
    },
    "fnInitComplete": function (oSettings, json) {
    }
});

//添加菜单
function AddMenu() {
    var JsonData = {
        MenuName : $('#AddForm .MenuName').val(),
        UrlAction : $('#AddForm .UrlAction').val(),
        CssClass : $('#AddForm .CssClass').val(),
        Sort : $('#AddForm .Sort').val()
    };
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/menu-create',
        data: JsonData,
        success: function (ret) {
            $("#AddMenu").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#menu_List').DataTable();
                table.ajax.reload();
                layer.msg('添加成功！');
            }else{
                layer.msg('添加失败！');
            }
        }
    });
}

//修改菜单
function _editFun(MenuId,MenuName,UrlAction,CssClass,Sort){
    //var JsonData = {
    //    MenuId:MenuId,
    //    MenuName:MenuName,
    //    UrlAction:UrlAction,
    //    CssClass:CssClass,
    //    Sort:Sort
    //};
   $("#EditForm").attr("data-menuid",MenuId);
   $('#EditMenu .MenuName').val(MenuName);
   $('#EditMenu .UrlAction').val(UrlAction);
   $('#EditMenu .CssClass').val(CssClass);
   $('#EditMenu .Sort').val(Sort);
    $("#EditMenu").modal('show');

}
function EditMenu() {
    var JsonData = {
        MenuId: $("#EditForm").attr("data-menuid"),
        MenuName : $('#EditMenu .MenuName').val(),
        UrlAction : $('#EditMenu .UrlAction').val(),
        CssClass : $('#EditMenu .CssClass').val(),
        Sort : $('#EditMenu .Sort').val()
    };
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/menu-update',
        data: JsonData,
        success: function (ret) {
            $("#EditMenu").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#menu_List').DataTable();
                layer.msg('修改成功！');
                table.ajax.reload();
            }else{
                layer.msg('修改失败！');
            }
        }
    });
}

/*菜单相关操作 end */


/*模块相关操作 start*/
//获取模块列表
function GetViewList(menuId){
    if ($("#action_List").hasClass("dataTable")) {
        dttable = $("#action_List").dataTable();
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
    }
    $("#SaveRelationShip").attr("data-menuid",menuId);
    $(".mapbtn").show();
    var actionTable = $("#action_List").dataTable({
        "ajax": {
            "url":"/role/get-menu-map-list",
            "type":'post',
            "data": function (d) {
                //添加额外的参数传给服务器
                return  {MenuId:menuId};
            }},
        'bPaginate': true,
        "bDestory": true,
        "bRetrieve": true,
        "bFilter":false,
        "bSort": true,
        "bProcessing": true,
        "autoWidth": false,
        "searching":true,
        "aoColumns": [
            {"mDataProp": "ControllerId",
                'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){

                    $(nTd).html('<i class="themeicon-custom fa fa-square-o '+ (oData.IsSelected ? 'selected' : '')+'"data-cid="'+ oData.ControllerId+'"></i>');
                }
            },
            {"mDataProp": "ControllerName"},
            {"mDataProp": "ControllerCode"},
            {"mDataProp": "Sort",
                'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){
                    $(nTd).html('<input type="text" id="" class="form-control table-sort-input" value="'+ oData.Sort+'" style="">');
                }
            },
            {
                "mDataProp": "ControllerId",
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html("<button name='btnEdit' type='button' class='btn btn-xs btn-info' onclick='_editControllerFun(\"" + oData.ControllerId + "\",\"" + oData.ControllerName + "\",\"" + oData.ControllerCode + "\")'><i class='fa fa-edit'></i>编辑</button>&nbsp;&nbsp;")

                }
            },
        ],
        "fnCreatedRow": function (nRow, aData, iDataIndex) {
            //add selected class
            $(nRow).click(function () {
                if ($(this).hasClass('row_selected')) {
                    $(this).removeClass('row_selected');
                } else {
                    table.$('tr.row_selected').removeClass('row_selected');
                    $(this).addClass('row_selected');
                }
            });
        },
        "fnInitComplete": function (oSettings, json) {
        }
    });

}
//添加模块
function AddMap(){
    var JsonData = {
        ControllerName : $('#AddMapForm .ControllerName').val(),
        ControllerCode : $('#AddMapForm .ControllerCode').val()
    };
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/map-create',
        data: JsonData,
        success: function (ret) {
            $("#AddMap").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#action_List').DataTable();
                table.ajax.reload();
                layer.msg('添加成功！');
            }else{
                layer.msg('添加失败！');
            }
        }
    });
}
//修改模块
function _editControllerFun(ControllerId,ControllerName,ControllerCode) {
    $("#EditMapForm").attr('data-cid', ControllerId);
    $("#EditMapForm .ControllerName").val(ControllerName);
    $("#EditMapForm .ControllerCode").val(ControllerCode);
    $("#EditMap").modal('show');
}
function EditMap(){
    var JsonData = {
        ControllerId: $("#EditMapForm").attr('data-cid'),
        ControllerName : $('#EditMapForm .ControllerName').val(),
        ControllerCode : $('#EditMapForm .ControllerCode').val()
    };
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/map-update',
        data: JsonData,
        success: function (ret) {
            $("#EditMap").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#action_List').DataTable();
                table.ajax.reload();
                layer.msg('修改成功！');
            }else{
                layer.msg('修改失败！');
            }
        }
    });
}

//保存关联操作
function SaveRelationShip(obj){
    var MenuId = $(obj).attr("data-menuid");
    var JsonData = {MenuId : MenuId};
    var MenuControllers = new Array();
    var dttable = $("#action_List").dataTable();
    var tableList = dttable.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
    $(tableList).each(function(k,v){
       var isSelected =  $(v).find("td").eq(0).find("i").hasClass("selected");
        if(isSelected == true){
            var ControllerId =  $(v).find("td").eq(0).find("i").data("cid");
            var ControllerName =  $(v).find("td").eq(1).html();
            var ControllerCode =  $(v).find("td").eq(2).html();
            var Sort =  $(v).find("td").eq(3).find("input").val();
            var data = {
                ControllerId:ControllerId,
                ControllerCode:ControllerCode,
                ControllerName:ControllerName,
                MenuId :MenuId,
                IsSelected :false,
                Sort:Sort
            }
            MenuControllers.push(data);
        }
    });
    JsonData.MenuControllers = MenuControllers;
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/map-save-relationship',
        data: JsonData,
        success: function (ret) {
            if(ret.IsBizSuccess){
                layer.msg('保存操作成功！');
            }else{
                layer.msg('操作操作失败！');
            }
        }
    });
};

/*模块相关操作 end*/



//重置表单
function resetFrom() {
    $('form').each(function (index) {
        $('form')[index].reset();
    });
}
//选中
$(document).on('click','.themeicon-custom',function(){
    $(this).hasClass("selected") ? $(this).removeClass("selected") : $(this).addClass("selected");
});
